package com.zd.mole.site.mohurd.process;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.springframework.stereotype.Component;

import com.zd.mole.process.ProcessHandler;
import com.zd.mole.site.mohurd.entity.Ot_achievement_info;
import com.zd.mole.site.mohurd.service.SysDictService;
import com.zd.mole.task.RequestMethod;
import com.zd.mole.task.TaskStatus;
import com.zd.mole.task.entity.Task;
import com.zd.mole.task.service.TaskService;
import com.zd.mole.utils.RegexUtils;

/**
 * 人员信息\个人工程业绩
 * @author ZhangDi
 *
 */
@Component
@Transactional
public class MohurdDataserviceQueryStaffStaffPerformanceListSysHandler implements ProcessHandler {

	
	@PersistenceContext 
	private EntityManager em;
	
	@Resource
	private TaskService taskService;
	
	@Resource
	private SysDictService sysDictService;
	
	@Override
	public List<Task> handler(Task task, String text) {
		
		//翻页
		int pg = 0;
		int pgsz = 0;
		String pgRegex = "<form class='pagingform' method='post' name='\\$pgfm' "
				+ "action='/dataservice/query/staff/staffPerformanceListSys/\\d+'></form><script>__pgfm\\('',"
				+ "\\{\"\\$total\":(?<total>\\d+),"
				+ "\"\\$reload\":(?<reload>\\d+),"
				+ "\"\\$pg\":(?<pg>\\d+),"
				+ "\"\\$pgsz\":(?<pgsz>\\d+)\\}\\)</script> <a sf='pagebar' sf:data=\""
				+ "\\(\\{pg:\\d+,ps:\\d+,tt:\\d+,pn:\\d+,pc:\\d+,id:'',st:true\\}\\)\"></a></div>"; 
		Pattern p2 = Pattern.compile(pgRegex);
		Matcher m2 = p2.matcher(text);
		if(m2.find()) {
			int total = Integer.parseInt(m2.group("total"));
			pg = Integer.parseInt(m2.group("pg"));
			pgsz =Integer.parseInt( m2.group("pgsz"));
			
			if(pg * pgsz < total) {
				String reload = m2.group("reload");
				String param ="$reload=" + reload
							+ "&$total=" + total
							+ "&$pg=" + (pg + 1)
							+ "&$pgsz=" + pgsz;
				
				Task nextPageTask = new Task();
				nextPageTask.setCode(task.getCode());
				nextPageTask.setParam(param);
				nextPageTask.setMethod(RequestMethod.POST);
				nextPageTask.setHostUrl(task.getHostUrl());
				nextPageTask.setRequestUrl(task.getRequestUrl());
				nextPageTask.setStatus(TaskStatus.Ready);
				nextPageTask.setProcessHandlerClassName(getClass().getSimpleName());
				taskService.save(nextPageTask);
			}
		}
		
		String tbody = RegexUtils.find("<tbody>" + INNER_HTML + "</tbody>", text);
		String regex = "<tr>\\s*<td data-header=\"序号\">" + INNER_HTML + "</tr>";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(tbody);
		int i = pgsz * (pg - 1);
		while(m.find()) {
			String tr = m.group(1);
			Ot_achievement_info ai = new Ot_achievement_info();
			String id = task.getCode();
			if(i > 0) {
				id += i;
			}
			i++;
			ai.setId(id);
			ai.setCustid(task.getCode());
			ai.setProjectNo(RegexUtils.find("<td data-header=\"项目编码\">(\\d+)</td>", tr));
			
			String projectName = RegexUtils.find("<td data-header=\"项目名称\" style=\"text-align:left;\">"
					+ "<a href=\"javascript:;\" onclick=\"top.window.location.href='/dataservice/query/project/projectDetail/\\d+'\">" 
					+ CN_REGEX + "</a></td>", tr);
			if(projectName == "") {
				projectName = RegexUtils.find("<td data-header=\"项目名称\" style=\"text-align:left;\">" + CN_REGEX + "</td>", tr);	
			}
			ai.setProjectName(projectName);
			
			String projectArea2 = RegexUtils.find("<td data-header=\"项目属地\" style=\"text-align:left;\">" + CN_REGEX + "</td>", tr);
			String projectArea = RegexUtils.find("^([\u4e00-\u9fa5]+)-.*", projectArea2);
			projectArea = sysDictService.findOrSaveByTypeLabel("oLife_ComAreaScope", projectArea, "企业注册属地");
			ai.setProjectArea(projectArea);
			ai.setProjectArea2(projectArea2);
			
			String projectType = RegexUtils.find("<td data-header=\"项目类别\">" + CN_REGEX + "</td>", tr);
			projectType = sysDictService.findOrSaveByTypeLabel("oLife_ProjectType", projectType, "项目类别");
			ai.setProjectType(projectType);
			ai.setDevelopmentDw(RegexUtils.find("<td data-header=\"建设单位\" style=\"text-align:left;\">" + CN_REGEX_NO_S + "</td>", tr));
			
			ai.setCreate_by("1");
			ai.setUpdate_by("1");
			Date now = new Date();
			ai.setCreate_date(now);
			ai.setUpdate_date(now);
			ai.setDel_flag("0");
			em.merge(ai);
			
			Task newTask = new Task();
			newTask.setCode(ai.getProjectNo());
			newTask.setHostUrl(task.getHostUrl());
			newTask.setMethod(RequestMethod.GET);
			newTask.setProcessHandlerClassName("MohurdDataserviceQueryProjectProjectDetailHandler");
			newTask.setRequestUrl("/dataservice/query/project/projectDetail/" + ai.getProjectNo());
//			newTask.setStatus(TaskStatus.Ready);
			newTask.setRemark(task.getCode());
			taskService.save(newTask);
		}
		
		
		
		return null;
	}

}
